﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the file system profile entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - Quota
    ///    | - QuotaUsage
    ///    | - QuotaRemainder
    ///    | - FileCount
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     | - Account(of the same level)
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class FileOwnerEntity : AccountProfileEntity
    {
        #region Fields

        private ICollection<FileEntity> _files;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public FileOwnerEntity()
        {
            _files = new List<FileEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the quota.
        /// </summary>
        public virtual long Quota
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the quota usage.
        /// </summary>
        public virtual long QuotaUsage
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the file count.
        /// </summary>
        public virtual int FileCount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual FileOwnerFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the collection of files.
        /// </summary>
        public virtual ICollection<FileEntity> Files
        {
            get
            {
                return _files;
            }
            set
            {
                _files = value;
            }
        }

        #endregion

        #region Custom Properties

        /// <summary>
        /// Gets the quota remainder.
        /// </summary>
        public virtual long QuotaRemainder
        {
            get
            {
                return Quota - QuotaUsage;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Quota"] = Quota;
                obj["QuotaUsage"] = QuotaUsage;
                obj["QuotaRemainder"] = QuotaRemainder;
                obj["FileCount"] = FileCount;
                obj["Flags"] = Flags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class FileOwnerMapping : EntityMapping<FileOwnerEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public FileOwnerMapping()
        {
            Table("fwk_FileOwners");
            Id(x => x.EntityID, m =>
            {
                m.Column("OwnerID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Account, m =>
            {
                m.Column("AccountID");
                m.ForeignKey("FK_fwk_FileOwners_fwk_Accounts");
                m.Index("IX_fwk_FileOwners");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.Quota, m =>
            {
                m.Column("[Quota]");
                m.NotNullable(true);
            });
            Property(x => x.QuotaUsage, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.FileCount, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Files, m =>
            {
                m.Table("fwk_Files");
                m.Key(k =>
                {
                    k.Column("OwnerID");
                    k.ForeignKey("FK_fwk_Files_fwk_FileOwners");
                    k.NotNullable(true);
                });
                m.Inverse(true);
            },
            a =>
            {
                a.OneToMany();
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
